//
//  BIDSuggestionAndPraiseListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/22.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDSuggestionAndPraiseListViewController.h"
#import "BIDSuggestionAndPraiseCell.h"
#import "BIDSuggestionAndPraiseDetailViewController.h"

static NSString *suggestionAndPraiseListURL = @"feedbackAndroid/list.action";

@interface BIDSuggestionAndPraiseListViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL _bRegister;
}
@end

@implementation BIDSuggestionAndPraiseListViewController
@synthesize isSuggestion;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"历史记录";
    // Do any additional setup after loading the view.
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], suggestionAndPraiseListURL];
    NSString *strPost = @"";
    NSString *strType = isSuggestion ? @"1" : @"2";
    strPost = [[NSString alloc] initWithFormat:@"searchParam.type=%@&memberId=%@&page=%ld&rows=%ld", strType, [AppDelegate getUserId], self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
        [self autoDecideToShowEmptyView];
    }];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDSuggestionAndPraiseCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    NSDictionary *dic = self.dataSourceArr[row];
    BIDSuggestionAndPraiseCell *suggestionAndPraiseCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    suggestionAndPraiseCell.dateLabel.text = dic[@"createTime"];
    suggestionAndPraiseCell.contentLabel.text = dic[@"content"];
    NSArray *replyArr = dic[@"replyList"];
    NSUInteger count = replyArr.count;
    suggestionAndPraiseCell.messageCountLabel.text = [[NSString alloc] initWithFormat:@"%ld", count];
    cell = suggestionAndPraiseCell;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    BIDSuggestionAndPraiseDetailViewController *vc = [[BIDSuggestionAndPraiseDetailViewController alloc] initWithNibName:@"BIDSuggestionAndPraiseDetailViewController" bundle:nil];
    vc.suggestionID = dic[@"id"];
    vc.suggestDate = dic[@"createTime"];
    vc.suggestContent = dic[@"content"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 76.f;
}

@end
